package com.kolon.business.mResourceRes.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.DateUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonNews.controller.MResourceResWriteController")
public class MResourceResWriteController implements SimpleController {

	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mResourceRes/mResourceResWrite.do")
	public ModelAndView controller(HttpServletRequest request,
			
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String scriptType 	= "";
		String message   	= "";
		String message_url	= "";
		String ResType = ComUtil.getNullCheck(request.getParameter("ResType"));
		String ResTypeNM = ComUtil.getNullCheck(request.getParameter("ResTypeNM"));
		String ResTypeCatid = ComUtil.getNullCheck(request.getParameter("ResTypeCatid"));
		String type = request.getParameter("Type");
		// 부서자원도 COMPANY로 등록해야함.
		if("DEPT".equals(type)) {
			type = "COMPANY";
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("ResType", ResType);
		modelAndView.addObject("ResTypeNM", ResTypeNM);
		modelAndView.addObject("ResTypeCatid", ResTypeCatid);
		if(!"".equals(request.getParameter("subject")) && request.getParameter("subject")!=null) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);			
			buffer.append("/resource_res.aspx?mode=").append("3");
			buffer.append("&subject=").append(ComUtil.getEncode(request.getParameter("subject"),"EUC_KR"));
			buffer.append("&startdate=").append(request.getParameter("startdate"));
			buffer.append("&endate=").append(request.getParameter("startdate"));
			buffer.append("&memo=").append(ComUtil.getEncode(request.getParameter("memo"),"EUC-KR"));
			buffer.append("&ResourceNM=").append(ComUtil.getEncode(request.getParameter("ResourceNM"),"EUC-KR"));
			buffer.append("&ToUserEmail=").append("");
			buffer.append("&ToUserInfo=").append("");
			buffer.append("&mailYN=").append("Y");
			buffer.append("&ResourceID=").append(request.getParameter("ResourceID"));
			buffer.append("&StartHour=").append(request.getParameter("StartHour"));
			buffer.append("&StartMin=").append(request.getParameter("StartMin"));
			buffer.append("&EndHour=").append(request.getParameter("EndHour"));
			buffer.append("&EndMin=").append(request.getParameter("EndMin"));
			buffer.append("&Type=").append(type);
			buffer.append("&repeat=").append("N");
			buffer.append("&MagYN=").append("N");
			buffer.append("&MagEmail=").append("");
			buffer.append("&MagMobileNum=").append("");
			buffer.append("&MagName=").append("");
			buffer.append("&strSMSYN=").append("N");
			buffer.append("&userid=").append(id);
			buffer.append("&pw=").append(pw);
			// XML DOCUMENT 및 인증 처리
			String returnText = ComUtil.getDocumentText(buffer.toString(), id, pw);
			if(returnText.indexOf("중복") == -1) { //예약등록 성공
				scriptType 	= "003";
				message   	= "예약이 등록되었습니다.";
				if(ResType.equals("View")){//상세보기
					message_url = "/business/mResourceRes/mResourceResView.do?";
					message_url	= message_url + "year="+request.getParameter("startdate").replaceAll("-", "").substring(0, 4)
					+"&month="+request.getParameter("startdate").replaceAll("-", "").substring(4, 6)+"&day="+request.getParameter("startdate").replaceAll("-", "").substring(6, 8)
					+"&strResourceType="+request.getParameter("Type")+"&strResourceID="+request.getParameter("ResourceID")
					+"&ResourceNM="+request.getParameter("ResourceNM");
				}else{ //리스트형 보기
					message_url = "/business/mResourceRes/mResourceResDetailList.do?ResourceType=&catid="+ResTypeCatid;
					message_url	= message_url + "&year="+request.getParameter("startdate").replaceAll("-", "").substring(0, 4)
					+"&month="+request.getParameter("startdate").replaceAll("-", "").substring(4, 6)+"&day="+request.getParameter("startdate").replaceAll("-", "").substring(6, 8)
					+"&type="+request.getParameter("Type")+"&strResourceID="+request.getParameter("ResourceID")
					+"&ResourceNM="+ComUtil.getEncode(request.getParameter("ResTypeNM"),"UTF-8");
				}
				modelAndView.addObject("scriptType", scriptType);
				modelAndView.addObject("message", message);
				modelAndView.addObject("message_url", message_url);
				modelAndView.setViewName("/business/common/alert");
			}else{ //예약등록 실패
				scriptType 	= "002";
				message   	= "동일한 시간에 예약정보가 존재합니다.";
				modelAndView.addObject("scriptType", scriptType);
				modelAndView.addObject("message", message);
				//modelAndView.addObject("message_url", message_url);
				modelAndView.setViewName("/business/common/alert");
			}
		}else{
			modelAndView.addObject("ResourceNM", request.getParameter("ResourceNM"));
			modelAndView.addObject("ResourceDate", request.getParameter("ResourceDate")); //회의일자를 받음
			modelAndView.addObject("catid", request.getParameter("catid"));
			modelAndView.addObject("type", request.getParameter("type"));
			modelAndView.addObject("selTime", request.getParameter("selTime")); //시작시간
			modelAndView.addObject("selEndTime", request.getParameter("selEndTime")); //종료시간
			modelAndView.addObject("nHour", DateUtil.getHourStr());
		}
		return modelAndView;
	}

}